#ifndef __TRIGRID_H__
#define __TRIGRID_H__

#include "MinMax2DBox.h"
#include "../util/NTuple.h"
#include "Triangle.h"
#include <vector>


namespace geom
{
    typedef std::vector<geom::Triangle> Cell;

	class TriGrid
	{
		public:
                        TriGrid() : m_gridArray(), m_gridBox(Point2D(0,0)) , m_gridCellsX(-1), m_gridCellsY(-1) {}
			TriGrid(const MinMax2DBox &box, size_t numPoints, float desiredPointsPerCell);
            ~TriGrid() { clear();}
            void clear();


			void addTriangle(const Point3D &p1, const Point3D &p2, const Point3D &p3) {addTriangle(geom::Triangle(p1,p2,p3));}
			void addTriangle(const Triangle &t);
			float getHeightAt(float x, float y) const;

		private:
            inline int twoDToArray(int x, int y) const {return (x + y*m_gridCellsX);}
            inline int twoDToArray(util::Pair<int> p) const {return (p[0] + p[1]*m_gridCellsX);}
			util::Pair<int> calculateCellForPoint(float x, float y) const;
            std::vector<Cell> m_gridArray; // bidimensional array, with each cell containing a vector of tris
			MinMax2DBox m_gridBox;
			int m_gridCellsX;	// size of the grid in x direction
			int m_gridCellsY;	// size of the grid in y direction
			float m_minPointHeight;
	};
}

#endif
